var interval; // 记时器
var audio
var audioplayUtil = require("../../utils/audioplay.js")
var app = getApp();
var currentIndex;
Page({
  data: {
    authdialog: getApp().globalData.source + "dialog/dialog.png",
    classfilyDataListProgram: [],
    indexNumberProgram: null, //当前播放音频下标
    showloading: true,
    classfilyDataList: [],
    indexNumber: null, //当前播放音频下标
    timer: '', //播放时间 播放时间统计
    currentPosition: 0, //选定音频的播放位置
    duration: 0, //选定音频的长度
    history: false,
    showAuth: false,
    buyed: false,
    collection: true,
    play: true,
    playindex: 0,
    page: 1,
    pagesize: 5,
    showbottom: false,
    bottomtext: '',
    authbg: getApp().globalData.authImg,
    authtitle: getApp().globalData.source + "red_packet/title1.png",
    authbtn2: getApp().globalData.source + "red_packet/btn2.png",
    maxpagesize: 1,
    findmask: true
  },
  bookstoreclick:function(){
    getApp().showMsgToast("敬请期待");
  },

  collect: function(event) {
    var index = event.currentTarget.dataset.index;
    currentIndex = index;
    this.dealCollect("1")
  },
  uncollect: function(event) {
    var index = event.currentTarget.dataset.index;
    currentIndex = index;
    this.dealCollect("2")
  },


  // 处理收藏
  dealCollect: function(status) {
    this.setData({
      showmask: true
    })
    var vm = this
    var dataparm;
    dataparm = {
      "programId": vm.data.classfilyDataListProgram[currentIndex].id,
      "status": 2,
      "userId": wx.getStorageSync("userId")
    }
    if (status == "1") {
      getApp().requestLoading(getApp().globalData.https + "programResource/mark",
        "post", dataparm, "执行中..",
        function(res) {
          if (res.code == 0) {
            vm.showDialog()
            vm.setData({
              page: 1
            })
            vm.getdata(1)
          }
        });
    } else {
      getApp().requestLoading(getApp().globalData.https + "programResource/delHistoryMark",
        "post", dataparm, "执行中..",
        function(res) {
          if (res.code == 0) {
            vm.setData({
              page: 1
            })
            vm.getdata(1)
          }
        });
    }
  },

  showDialog: function () {
    this.dialog.showDialog();
  },
  // 取消收藏
  cancelCollect: function() {
    this.hidemask()
    var vm = this
    var dataparm;
    dataparm = {
      "programId": vm.data.classfilyDataListProgram[currentIndex].id,
      "seasonId": 0,
      "status": 2,
      "userId": wx.getStorageSync("userId")
    }
    getApp().requestLoading(getApp().globalData.https + "programResource/delHistoryMark",
      "post", dataparm, "正在取消",
      function(res) {
        if (res.code == 0) {
          var list = vm.data.classfilyDataListProgram;
          list.splice(currentIndex, 1);
          vm.setData({
            classfilyDataListProgram: list
          })
        }
      });

  },

  hidemask: function() {
    this.setData({
      findmask: true
    })
  },
  find_same: function() {
    this.hidemask()
    var vm = this
    wx.navigateTo({
      url: '/pages/find_same/find_same?categoryId=' + vm.data.classfilyDataListProgram[currentIndex].categoryId,
    })
  },
  // 置顶
  zhiding: function() {
    this.hidemask()
    var vm = this
    var dataparm;
    dataparm = {
      "programId": vm.data.classfilyDataListProgram[currentIndex].id,
      "userId": wx.getStorageSync("userId")
    }
    getApp().requestLoading(app.globalData.https + "programResource/topMark",
      "get", dataparm, "置顶中..",
      function(res) {
        if (res.code == 0) {
          vm.getdata(2)
        }
      });

  },

  authbtn: function() {
    var vm = this
    wx.showLoading({
      title: "正在检查网络.."
    })
    this.setData({
      authbtn: true
    })
  },

  historyclick: function() {
    wx.navigateTo({
      url: '/pages/record_history/record_history',
    })
  },
  buyedclick: function() {
    wx.navigateTo({
      url: '/pages/buyed_history/buyed_history',
    })
  },
  tohome: function() {
    wx.switchTab({
      url: '/pages/style/style',
    })
  },
  showmask: function(event) {
    var index = event.currentTarget.dataset.index;
    currentIndex = index;
    this.setData({
      findmask: false
    })
  },
  cancelmask: function() {
    this.setData({
      findmask: true
    })
  },

  bindGetUserInfo: function(e) {
    getApp().authInfo(e);
  },


  getdata: function(status) {
    var vm = this
    var data;
    if (wx.getStorageSync("platform") != "1") {
      data = {
        "startPoint": vm.data.page,
        "maxPage": vm.data.pagesize,
        "userId": wx.getStorageSync("userId"),
        "status": status,
        "platform": 0
      }
    } else {
      data = {
        "startPoint": vm.data.page,
        "maxPage": vm.data.pagesize,
        "userId": wx.getStorageSync("userId"),
        "status": status
      }
    }

    if (status == 1) {
      data = {
        "userId": wx.getStorageSync("userId")
      }
      getApp().requestLoading(getApp().globalData.https + "programResource/record/recommendProgram",
        "get", data, "加载中..",
        function(res) {
          wx.stopPullDownRefresh()
          if (res.code == 0) {
            vm.setData({
              classfilyDataListProgram: vm.data.page == 1 ? res.data : vm.data.classfilyDataListProgram.concat(res.data),
              showloading: false
            })
          } else {
            vm.setData({
              classfilyDataListProgram: [],
              bottomtext: '暂无推荐栏目',
              showloading: false,
            })
          }
        },
        function(res) {
          console.log(res)
        });
    } else {
      getApp().requestLoading(getApp().globalData.https + "programResource/status4Mark/" + wx.getStorageSync("userId") + "/" + status,
        "get", data, "加载中..",
        function(res) {
          wx.stopPullDownRefresh()
          if (res.code == 0) {
            vm.setData({
              classfilyDataListProgram: vm.data.page == 1 ? res.data.data : vm.data.classfilyDataListProgram.concat(res.data.data),
              totalPages: res.data.totalPages,
              showloading: false
            })
          } else {
            vm.setData({
              classfilyDataListProgram: [],
              bottomtext: '还没有收藏记录哦.',
              showloading: false,
            })
          }
        },
        function(res) {
          console.log(res)
        });
    }


  },

  bottombar: function() {},

  // 点击历史
  history: function() {
    if (wx.getStorageSync("userId") == "" || wx.getStorageSync("userId") == undefined) {
      vm.setData({
        bottomtext: '还没有登录哦.'
      })
    } else {
      this.setData({
        showloading: true,
        history: true,
        buyed: false,
        collection: false,
        bottomtext: "",
        indexNumberProgram: -1,
        classfilyDataListProgram: [],
        page: 1
      })
      this.getdata(1)
    }
  },


  // 点击收藏
  collection: function() {
    if (wx.getStorageSync("userId") == "" || wx.getStorageSync("userId") == undefined) {
      vm.setData({
        bottomtext: '还没有登录哦.'
      })
    } else {
      this.setData({
        showloading: true,
        history: false,
        buyed: false,
        bottomtext: "",
        indexNumberProgram: -1,
        classfilyDataListProgram: [],
        collection: true,
        page: 1
      })
      this.getdata(2)
    }
  },


  // 猜你喜欢
  guess: function() {
    wx.navigateTo({
      url: './guess/guess',
    })
  },


  /**
   * 跳转到播放详情页面
   */
  todetails: function(e) {
    var season = e.currentTarget.dataset.season;
    var id = e.currentTarget.dataset.id;
    var title = e.currentTarget.dataset.title; //节目标题 

    var flag = e.currentTarget.dataset.flag
    if (flag != "0") {
      wx.showToast({
        title: '该栏目已下架',
      })
      return
    }
    var vm = this
    //热听绑定、有声小说等、、、点击埋点 
    vm.setData({
      userIdss: wx.getStorageSync("userId"), //用户编号
      homeClassifyProgramId: id, //热听榜等。。栏目点击Id记录
      homeClassifyProgramName: title, //热听榜。。等栏目点击Name记录
    })
    wx.navigateTo({
      url: "/pages/day_day_listen/details/details?id=" + id + "&season=" + season,
      success: function() {},
      fail: function() {}
    })
  },

  shareclick: function() {

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.dialog = this.selectComponent("#dialog");
  },

  // 最新已购数量
  getBuyedCount: function() {
    var date = wx.getStorageSync("buyedtime")
    var vm = this
    var dataparm;
    if (date == undefined) {
      dataparm = {
        "userId": wx.getStorageSync("userId")
      }
    } else {
      dataparm = {
        "userId": wx.getStorageSync("userId"),
        "startTime": date
      }
    }
    getApp().requestLoading(getApp().globalData.https + "userResource/recordBuyCount",
      "get", dataparm, "更新中..",
      function(res) {
        if (res.code == 0) {
          vm.setData({
            buycount: "·" + res.data.count
          })
        }
      });

  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (wx.getStorageSync("userId") == "" || wx.getStorageSync("userId") == undefined) {
      if (wx.getStorageSync("tourist") == "1") {
        if (wx.getStorageSync("authtype") == "0") {
          this.setData({
            showAuth: true,
            showAuth2: false
          })
        } else {
          this.setData({
            showAuth: false,
            showAuth2: true
          })
        }
        return
      }
    } else {
      this.setData({
        showAuth: false,
        showAuth2: false
      })
    }
    this.getBuyedCount()
    var vm = this;
    vm.setData({
      showloading: true,
      page: 1,
      classfilyDataListProgram: []
    })
    if (vm.data.history) {
      vm.getdata(1)
    }
    if (vm.data.collection) {
      vm.getdata(2)
    }


  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var vm = this
    if (!vm.data.collection) return
    if (vm.data.page >= vm.data.totalPages) {
      vm.setData({
        showbottom: true,
        bottomtext: '没有更多内容了'
      })
      return
    }
    this.setData({
      page: vm.data.page + 1
    })
    if (vm.data.history) {
      vm.getdata(1)
    }
    if (vm.data.collection) {
      vm.getdata(2)
    }
  },
})